package es.ceura.wilder.actions;

import es.ceura.wilder.datastructures.PostVars;
import es.ceura.wilder.datastructures.request.HTTPRequest;
import es.ceura.wilder.resources.DeniedAccesException;
import java.net.Socket;

/**
 *
 * @author Jorge Madrid
 */
public class Post extends Get {

    private static final String REGEX_POST = "([^ &]+=[^ &]+&[^ &]+=[^ &]+)+|([^ &]+=[^ &]+)";

    @Override
    public void doAction(HTTPRequest request, Socket socket) throws
            DeniedAccesException {
        PostVars postVars = parsePostMessage(request.getMessage());
        System.out.println("postVars = " + postVars);
        super.doAction(request, socket);
    }

    private PostVars parsePostMessage(String message) {
        PostVars postVars = new PostVars();

        if (message.matches(REGEX_POST)) {
            String[] pairs = message.split("&");
            for (String pair : pairs) {
                String[] var = pair.split("=");
                final String key = var[0];
                final String value = var[1];
                postVars.putVar(key, value);
            }
        } else {
            //??
        }
        return postVars;
    }
}
